﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SqlServer = DataCoreTechnologies.SqlServer;

namespace DataCoreTechnologies.Scribe.Business.Html
{
    internal class IndexDocument : Document
    {
        #region Public
        //Constructor
        internal IndexDocument(SqlServer.Index index)
        {
            DocumentType = DocumentType.Index;
            _index = index;

            if (index.View == null)
            {
                RootDatabase = index.Table.Schema.Database;
                RootServer = RootDatabase.Server;
                URL = "index." + RootDatabase.Name + "." + 
                    index.Table.Schema.Name + "." + index.Table.Name + ".index." + 
                    index.Name + ".html";
            }
            else
            {
                RootDatabase = index.View.Schema.Database;
                RootServer = RootDatabase.Server;
                URL = "index." + RootDatabase.Name + "." +
                    index.View.Schema.Name + "." + index.View.Name + ".index." +
                    index.Name + ".html";
            }
        }
        //Properties
        internal override string DisplayName { get { return _index.Name; } }
        //Methods
        internal override string GetHtml()
        {
            if (_html == "")
            {
                AddCssLine(@"      	.propertyColumn {width: 300px; word-wrap: break-word;}");
                AddCssLine(@"      	.nameColumn {width: 300px; word-wrap: break-word;}");

                AddCssLine(@"      	.orderColumn { width: 15%; }");
                AddCssLine(@"      	.typeColumn { width: 30%; }");

                AddBodyLine(@"<div id=""mainBody"">");
                Boolean evenRow = true;

                AddBodyLine(@"<h2 id=""title"">" + ScrubHtml(_index.Name) + @" (Index)</h2>");

                if (_index.Table != null)
                {
                    string heirarchy = @"<h2 id=""topNav"">Location: <a href=""http://index.html"">" + RootServer.Name + @"</a> . ";
                    heirarchy += @"<a href=""http://index." + RootDatabase.Name + @".html"">"
                        + ScrubHtml(RootDatabase.Name) + @"</a> . ";

                    if (Business.FilterPreferences.ShowSchemasPage)
                       heirarchy += @"<a href=""http://index." + RootDatabase.Name + @".schemas.html"">"
                           + ScrubHtml(_index.Table.Schema.Name) + @"</a> . ";
                    else
                       heirarchy += ScrubHtml(_index.Table.Schema.Name) + @" . ";

                    if (Business.FilterPreferences.ShowTablesPage)
                       heirarchy += @"<a href=""http://index." + RootDatabase.Name + @"." +
                           _index.Table.Schema.Name + @".table." + _index.Table.Name + @".html"">" +
                           ScrubHtml(_index.Table.Name) + @"</a> . ";
                    else
                       heirarchy += ScrubHtml(_index.Table.Name) + @" . ";

                    heirarchy += _index.Name + @"</h2>";
                    AddBodyLine(heirarchy);
                }
                else
                {
                    string heirarchy = @"<h2 id=""topNav"">Location: <a href=""http://index.html"">" + RootServer.Name + @"</a> . ";
                    heirarchy += @"<a href=""http://index." + RootDatabase.Name + @".html"">"
                        + ScrubHtml(RootDatabase.Name) + @"</a> . ";
                    
                    if (Business.FilterPreferences.ShowSchemasPage)
                       heirarchy += @"<a href=""http://index." + RootDatabase.Name + @".schemas.html"">"
                           + ScrubHtml(_index.View.Schema.Name) + @"</a> . ";
                    else
                       heirarchy += ScrubHtml(_index.View.Schema.Name) + @" . ";

                    if (Business.FilterPreferences.ShowViewsPage) 
                       heirarchy += @"<a href=""http://index." + RootDatabase.Name + @"." +
                           _index.View.Schema.Name + @".view." + _index.View.Name + @".html"">" +
                           ScrubHtml(_index.View.Name) + @"</a> . ";
                    else
                       heirarchy += ScrubHtml(_index.View.Name) + @" . ";

                    heirarchy += _index.Name + @"</h2>";
                    AddBodyLine(heirarchy);
                }
                #region Properties
                AddBodyLine(@"<h2>");
                AddBodyLine(@"<img id=""img_properties"" src=""" + RootFolderPath + @"minus.jpg""> ");
                AddBodyLine(@"<a href=""javascript:;"" onmousedown=""toggleDiv('properties');"">Properties</a>");
                AddBodyLine(@"</h2>");

                AddBodyLine(@"<div id=""properties"">");
                AddBodyLine(@"<table>");

                string colGroup = @"<colgroup><col class=""propertyColumn""><col class=""valueColumn""></colgroup>";
                AddBodyLine(colGroup);

                AddBodyLine(@"<tr><th>Property</th><th>Value</th></tr>");
                AddBodyLine(@"<tr class=""evenRow""><td>Name</td><td>" + ScrubHtml(_index.Name) + @"</td></tr>");
                string s = @"<tr class=""oddRow""><td>Key Type</td><td>";
                s += _index.Type == SqlServer.IndexType.Clustered ? "Clustered " : "NonClustered ";
                if (_index.IsPrimaryKey == true) s += "PrimaryKey ";
                if (_index.IsUnique == true) s += "UniqueConstraint";
                s += @"</td></tr>";
                AddBodyLine(s);
                s = @"<tr class=""evenRow""><td>Is Clustered</td><td>";
                s += _index.Type == SqlServer.IndexType.Clustered ? "True" : "False" + @"</td></tr>";
                AddBodyLine(s);
                AddBodyLine(@"<tr class=""oddRow""><td>Is Unique</td><td>" + _index.IsUnique.ToString() + @"</td></tr>");
                AddBodyLine(@"<tr class=""evenRow""><td>Ignore Duplicate Keys</td><td>" + _index.IgnoreDuplicateKey.ToString() + @"</td></tr>");
                AddBodyLine(@"<tr class=""oddRow""><td>Allow Row Locks</td><td>" + _index.AllowRowLocks.ToString() + @"</td></tr>");
                AddBodyLine(@"<tr class=""evenRow""><td>Allow Page Locks</td><td>" + _index.AllowPageLocks.ToString() + @"</td></tr>");

                s = @"<tr class=""oddRow""><td>Is Enabled</td><td>";
                s += _index.IsDisabled==false?"True":"False" + @"</td></tr>";
                AddBodyLine(s);

                if (_index.CreationDate == null)
                    AddBodyLine(@"<tr class=""evenRow""><td>Creation Date</td><td>N/A</td></tr>");
                else
                    AddBodyLine(@"<tr class=""evenRow""><td>Creation Date</td><td>" +  _index.CreationDate.ToString() + @"</td></tr>");

                if (_index.ModifyDate == null)
                    AddBodyLine(@"<tr class=""oddRow""><td>Last Modified</td><td>N/A</td></tr>");
                else
                    AddBodyLine(@"<tr class=""oddRow""><td>Last Motified</td><td>" + _index.ModifyDate.ToString() + @"</td></tr>");

                s = @"<tr class=""evenRow""><td>Description</td><td>";
                s += _index.Description == "" ? @"nbsp;" : ScrubHtml(_index.Description);
                s += @"</td></tr>";
                AddBodyLine(s);

                AddBodyLine(@"</table>");
                AddBodyLine(@"</div>");
                #endregion
                #region Columns
                AddBodyLine(@"<h2>");
                AddBodyLine(@"<img id=""img_parameters"" src=""" + RootFolderPath + @"minus.jpg""> ");
                AddBodyLine(@"<a href=""javascript:;"" onmousedown=""toggleDiv('columns');"">Columns</a>");
                AddBodyLine(@"</h2>");

                AddBodyLine(@"<div id=""columns"">");
                AddBodyLine(@"<table>");

                colGroup = @"<colgroup><col class=""nameColumn""><col class=""orderColumn"">" +
                    @"<col class=""directionColumn""><col class=""typeColumn""></colgroup>";
                AddBodyLine(colGroup);
                
                AddBodyLine(@"<tr><th>Name</th><th>Order</th><th>Direction</th><th>Type</th></tr>");

                var indexColumns = (from indexColumn in _index.IndexColumns
                                    orderby indexColumn.Order
                                    select indexColumn);
                evenRow = true;
                foreach (SqlServer.IndexColumn indexColumn in indexColumns)
                {
                    StringBuilder sb = new StringBuilder();
                    if (evenRow == true)
                        sb.Append(@"<tr class=""evenRow"">");
                    else
                        sb.Append(@"<tr class=""oddRow"">");
                    evenRow = !evenRow;

                    sb.Append(@"<td>" + ScrubHtml(indexColumn.Column.Name) + @"</td>");
                    sb.Append(@"<td>" + indexColumn.Order.ToString() + @"</td>");
                    if (indexColumn.IsDescending == false)
                        sb.Append(@"<td>Ascending</td>");
                    else
                        sb.Append(@"<td>Descending</td>");
                    
                    if (indexColumn.Column.UserType != null)
                        sb.Append(@"<td>" + ScrubHtml(indexColumn.Column.UserType.Name) + @"</td>");
                    else 
                        sb.Append(@"<td>" + ScrubHtml(indexColumn.Column.SystemType.Name) + @"</td>");
                    
                    AddBodyLine(sb.ToString());
                }

                AddBodyLine(@"</table>");
                AddBodyLine(@"</div>");
                #endregion
                #region Extended Properties
                AddBodyLine(@"<h2>");
                AddBodyLine(@"<img id=""img_extendedProperties"" src=""" + RootFolderPath + @"minus.jpg""> ");
                AddBodyLine(@"<a href=""javascript:;"" onmousedown=""toggleDiv('extendedProperties');"">Extended Properties</a>");
                AddBodyLine(@"</h2>");

                AddBodyLine(@"<div id=""extendedProperties"">");
                AddBodyLine(@"<table>");

                AddBodyLine(@"<tr><th>Property</th><th>Value</th></tr>");
                AddBodyLine(@"<colgroup><col class=""nameColumn""><col class=""valueColumn""></colgroup>");

                evenRow = true;
                foreach (SqlServer.ExtendedProperty extProperty in _index.ExtendedProperties)
                {
                    string row = evenRow == true ? @"<tr class=""evenRow"">" : @"<tr>";
                    evenRow = !evenRow;
                    row += @"<td>" + ScrubHtml(extProperty.Name) + @"</td><td>" + ScrubHtml(extProperty.Value) + @"</td></tr>";
                    AddBodyLine(row);
                }

                AddBodyLine(@"</table>");
                AddBodyLine(@"</div>");
                #endregion

                #region DDL
                #endregion

                AddBodyLine(@"</div>");
                _html = CreateHtmlDocument();
            }
            return _html;
        }
        #endregion
        #region Private
        private SqlServer.Index _index;
        #endregion
    }
}
